package jmu.cze.utl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class ResultUtil {
    public static String getResult(Boolean bool){
        if (bool) {
            return "{\"result\":1}";
        }
        return "{\"result\":0}";
    }

    public static String getNext(String next,int result){
        return "{\"result\":"+result+",\"next\": \""+next+"\"}";
    }

    public static String getJson(int result,String key, Object value) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//不使用时间差的方式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//自定义日期格式对象
        mapper.setDateFormat(sdf);//指定日期格式

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("result",result);
        if(key!=null)
            map.put(key,value);
        try {
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
